package com.pzr.soccer.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.pzr.soccer.model.Role;
import com.pzr.soccer.model.User;
import com.pzr.soccer.service.RoleService;

@Service
public class RoleServiceImpl implements RoleService{

	@Autowired
	MongoOperations operations;
	
	@Override
	public Role save(Role role) {
		// TODO Auto-generated method stub
	  Role save=operations.save(role);
		return save;
	}

	@Override
	public Role findRoleByUserIdAndName(String userId, String roleName) {
		// TODO Auto-generated method stub
		User user=operations.findOne(Query.query(Criteria.where("id").is(userId)), User.class);
		if(user==null) {
			return null;
		}
		Role role=operations.findOne(Query.query(Criteria.where("name").is(roleName).and("id").in(user.getRoleIds())), Role.class);
		return role;
	}

}
